﻿using System;
using System.IO;
using System.Text;
using Microsoft.Xna.Framework.Audio;
using MP3Sharp;

namespace Terraria.ModLoader.IO
{
	// Token: 0x020002E3 RID: 739
	internal static class WAVCacheIO
	{
		// Token: 0x06001C60 RID: 7264 RVA: 0x00435498 File Offset: 0x00433698
		internal static bool WAVCacheAvailable(string wavCachePath)
		{
			return File.Exists(WAVCacheIO.ModCachePath + Path.DirectorySeparatorChar.ToString() + wavCachePath);
		}

		// Token: 0x06001C61 RID: 7265 RVA: 0x004354C8 File Offset: 0x004336C8
		internal static void SaveWavStream(MemoryStream output, string wavCachePath)
		{
			Directory.CreateDirectory(WAVCacheIO.ModCachePath);
			using (FileStream fileStream = File.Create(WAVCacheIO.ModCachePath + Path.DirectorySeparatorChar.ToString() + wavCachePath))
			{
				output.WriteTo(fileStream);
			}
		}

		// Token: 0x06001C62 RID: 7266 RVA: 0x00435524 File Offset: 0x00433724
		internal static Stream GetWavStream(string wavCachePath)
		{
			return File.OpenRead(WAVCacheIO.ModCachePath + Path.DirectorySeparatorChar.ToString() + wavCachePath);
		}

		// Token: 0x06001C63 RID: 7267 RVA: 0x00435550 File Offset: 0x00433750
		internal static void ClearCache(string modName)
		{
			DirectoryInfo directoryInfo = Directory.CreateDirectory(WAVCacheIO.ModCachePath);
			foreach (FileInfo fileInfo in directoryInfo.EnumerateFiles(Path.GetFileNameWithoutExtension(modName) + "_*.wav"))
			{
				fileInfo.Delete();
			}
		}

		// Token: 0x06001C64 RID: 7268 RVA: 0x004355B8 File Offset: 0x004337B8
		internal static void DeleteIfOlder(string modFilename, string wavCacheFilename)
		{
			FileInfo fileInfo = new FileInfo(modFilename);
			DirectoryInfo directoryInfo = Directory.CreateDirectory(WAVCacheIO.ModCachePath);
			foreach (FileInfo fileInfo2 in directoryInfo.EnumerateFiles(Path.GetFileNameWithoutExtension(wavCacheFilename.Substring(0, wavCacheFilename.LastIndexOf('_'))) + "_*.wav"))
			{
				if (fileInfo2.Name == wavCacheFilename)
				{
					if (fileInfo2.LastWriteTime < fileInfo.LastWriteTime)
					{
						fileInfo2.Delete();
					}
				}
				else
				{
					fileInfo2.Delete();
				}
			}
		}

		// Token: 0x06001C65 RID: 7269 RVA: 0x00435660 File Offset: 0x00433860
		public static SoundEffect CacheMP3(string wavCacheFilename, Stream stream)
		{
			SoundEffect result;
			using (MemoryStream memoryStream = new MemoryStream())
			{
				using (MP3Stream mp3Stream = new MP3Stream(stream))
				{
					using (BinaryWriter binaryWriter = new BinaryWriter(memoryStream, Encoding.UTF8))
					{
						memoryStream.Position = 44L;
						mp3Stream.CopyTo(memoryStream);
						uint num = (uint)memoryStream.Length - 44u;
						memoryStream.Position = 0L;
						ushort num2 = (ushort)mp3Stream.ChannelCount;
						uint frequency = (uint)mp3Stream.Frequency;
						ushort value = num2 * 2;
						uint value2 = frequency * (uint)num2 * 2u;
						binaryWriter.Write("RIFF".ToCharArray());
						binaryWriter.Write(num + 36u);
						binaryWriter.Write("WAVE".ToCharArray());
						binaryWriter.Write("fmt ".ToCharArray());
						binaryWriter.Write(16);
						binaryWriter.Write(1);
						binaryWriter.Write(num2);
						binaryWriter.Write(frequency);
						binaryWriter.Write(value2);
						binaryWriter.Write(value);
						binaryWriter.Write(16);
						binaryWriter.Write("data".ToCharArray());
						binaryWriter.Write(num);
						memoryStream.Position = 0L;
						WAVCacheIO.SaveWavStream(memoryStream, wavCacheFilename);
						result = SoundEffect.FromStream(memoryStream);
					}
				}
			}
			return result;
		}

		// Token: 0x040018CB RID: 6347
		public static readonly string ModCachePath = string.Concat(new string[]
		{
			Main.SavePath,
			Path.DirectorySeparatorChar.ToString(),
			"Mods",
			Path.DirectorySeparatorChar.ToString(),
			"Cache"
		});
	}
}
